#include "Base.h"
#include "GameList.h"

int compGame(const void *pElement1,const void *pElement2)
{
	CGame *pGame1 = *(CGame **)pElement1;
	CGame *pGame2 = *(CGame **)pElement2;
	return pGame1->GetTableId() - pGame2->GetTableId();
}
CGameList::CGameList(void)
{
}


CGameList::~CGameList(void)
{
	m_listGames.DeleteAll();
}

BOOL CGameList::AddGame(CGame *pGame)
{
	return m_listGames.Insert(pGame, compGame);
}

BOOL CGameList::RemoveGame(CGame *pGame)
{
	int nPos = m_listGames.FindPos(pGame,compGame);
	if(nPos == -1)
	{
		return FALSE;
	}
	return m_listGames.RemoveAt(nPos);
}

CGame* CGameList::FindGame(DWORD dwTableId)
{
	CGame game;
	game.SetTableId(dwTableId);
	return m_listGames.Find(&game, compGame);
}

void CGameList::Tick()
{
	int nCount = m_listGames.GetCount();
	for(int i = nCount - 1; i >= 0; i--)
	{
		CGame *pGame = m_listGames[i];
		if(pGame == NULL)
		{
			WriteLog("CGameList::Tick pGame == NULL, TotalGameCount=%d, i=%d", nCount, i);
			continue;
		}
		if(pGame->GetStatus() == Game_Status_Over)
		{
			operator delete (pGame, "CGame", __FILE__, __LINE__);
			m_listGames.RemoveAt(i);
			continue;
		}
		else
		{
            m_listGames[i]->Tick();
		}
	}
}
